using System.Collections.Generic;
using NetProtocol.Enum;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 玩家在逻辑服的基础模块
    /// </summary>
    public class LogicPlayerModule
    {
        public static LogicPlayerModule Instance { get; } = new LogicPlayerModule();

        #region 数据同步

        public void SyncEvent_UpdatePlayerInfo(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            logicPlayer.UpdatePlayerData(eventPod.playerUpdates);
        }

        public void SyncEvent_IncPlayerParams(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            foreach (KeyValuePair<int, int> pair in eventPod.dicParams)
            {
                int preValue = logicPlayer.playerParams.GetValueOrDefault(pair.Key, 0);
                int newValue = preValue + pair.Value;
                logicPlayer.playerParams[pair.Key] = newValue;
            }
        }

        public void SyncEvent_UpdatePlayerParams(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            logicPlayer.playerParams.AddRange(eventPod.dicParams);
        }

        public void SyncEvent_UpdateHeroes(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            foreach (HeroPOD heroPod in eventPod.heroes)
            {
                if (logicPlayer.heroes.TryGetValue(heroPod.cid, out LogicHeroData heroData))
                {
                    heroData.SyncData(heroPod);
                }
                else
                {
                    heroData = new LogicHeroData(heroPod);
                    logicPlayer.heroes[heroPod.cid] = heroData;
                    logicPlayer.levelPlayer.AddHero(heroData);
                }
            }
        }

        public void SyncEvent_UpdateFormation(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            FormationPOD formationPod = eventPod.formation;
            if (formationPod.formId != logicPlayer.formation.formId) // 不关心的编队
            {
                return;
            }
            long oldId = logicPlayer.formation.leaderHeroId;
            logicPlayer.formation.SyncData(formationPod);
            logicPlayer.UpdateFormationCache();
            if (logicPlayer.formation.leaderHeroId != oldId)
            {
                Level level = logicPlayer.levelPlayer?.level;
                if (level != null)
                {
                    level.OnLeaderChanged(logicPlayer.id, oldId, logicPlayer.formation.leaderHeroId);
                }
            }
        }

        public void SyncEvent_AddPet(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            LogicPetData petData = new LogicPetData(eventPod.pet);
            logicPlayer.pets[petData.id] = petData;
        }

        public void SyncEvent_UpdatePet(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            PetPOD petPod = eventPod.pet;
            logicPlayer.pets[petPod.id].SyncData(petPod);
        }

        public void SyncEvent_RemovePet(LogicPlayer logicPlayer, DataSyncEventPOD eventPod)
        {
            foreach (long petId in eventPod.longParams)
            {
                logicPlayer.pets.Remove(petId);
            }
        }

        #endregion
    }
}